//
//  decodeViewController.m
//  JFTRequest
//
//  Created by 竣付通 on 2017/11/24.
//  Copyright © 2017年 junHaoPay. All rights reserved.
//

#import "decodeViewController.h"
#import "XHPopMenu.h"
#import "NSData+JftBase64.h"
#import "NSData+AES.h"
#import "JFTDES.h"
#import "configHeader.h"
#import "MD5.h"
#import "DDRSAWrapper.h"
#import "DDRSAWrapper+openssl.h"
#import "rsaViewController.h"
#import"HLMBProgressHUD.h"

@interface decodeViewController ()
@property (weak, nonatomic) IBOutlet UIButton *typeButton;
@property (nonatomic,strong) NSArray *dataArray;
@property (weak, nonatomic) IBOutlet UITextField *decodeTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *encryptTextFiled;
@end

@implementation decodeViewController
-  (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = @[@"DES",@"AES",@"RSA",@"MD5"];
    self.decodeTextFiled.text = @"1234567890";
}
// 加密
- (IBAction)encryptButtonDidClick:(UIButton *)sender {
    if ([self.decodeTextFiled.text isEqualToString:@""]) {
        NSLog(@"请添加需要加密的字符串");
        return;
    }
    NSString *str = self.decodeTextFiled.text;
    NSString *resultStr;
    if ([_typeButton.titleLabel.text isEqualToString:@"RSA"]) {
        
    }else if ([_typeButton.titleLabel.text isEqualToString:@"MD5"]) {
        resultStr = [MD5 md532BitLower:str];
        self.encryptTextFiled.text = resultStr;
    }else if ([_typeButton.titleLabel.text isEqualToString:@"DES"]) {
        
        resultStr = [JFTDES encodeToPercentEscapeString:str];
        resultStr = [JFTDES encryptWithContent:str type:0 key:JFTDESKEY];
        self.encryptTextFiled.text = resultStr;
        
    }else if ([_typeButton.titleLabel.text isEqualToString:@"AES"]) {
        
        NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSData *miWenData = [data AES256EncryptWithKey:JFTAESKEY iv:JFTAESIV];
        self.encryptTextFiled.text = [miWenData base64EncodedString];
    }
}
//解密
- (IBAction)decodeButtonDidClick:(UIButton *)sender {
    if ([self.encryptTextFiled.text isEqualToString:@""]) {
        NSLog(@"请添加需要加密的字符串");
        return;
    }
    NSString *str = self.encryptTextFiled.text;
    NSString *resultStr;
     if ([_typeButton.titleLabel.text isEqualToString:@"RSA"]) {
        
    }else if ([_typeButton.titleLabel.text isEqualToString:@"MD5"]) {
        //MD5加密不可逆
       [HLMBProgressHUD addMBProgressHUDinView:self.view hudMode:5 hideDelay:2 hudDetailText:@"MD5加密不可逆"];
    }else if ([_typeButton.titleLabel.text isEqualToString:@"DES"]) {
        
        resultStr = [JFTDES encodeToPercentEscapeString:str];
        resultStr = [JFTDES encryptWithContent:str type:1 key:JFTDESKEY];
        self.decodeTextFiled.text = resultStr;
        
    }else if ([_typeButton.titleLabel.text isEqualToString:@"AES"]) {
        
        NSData *data = [NSData dataFromBase64String:str];
        data = [data AES256DecryptWithKey:JFTAESKEY iv:JFTAESIV];
        resultStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        self.decodeTextFiled.text = resultStr;
    }
}

- (IBAction)typeButtonDidClick:(UIButton *)sender {
    NSMutableArray<__kindof XHPopMenuItem *> *tempArr = [NSMutableArray array];
    for (int i=0; i<_dataArray.count; i++) {
        NSString  *str = [self.dataArray objectAtIndex:i];
        typeof(self) __weak weakSelf = self;
        XHPopMenuItem *model = [[XHPopMenuItem alloc] initWithTitle:str image:nil block:^(XHPopMenuItem *item) {
            [weakSelf.typeButton setTitle:str forState:UIControlStateNormal];
            if ([str isEqualToString:@"RSA"]) {
                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
                rsaViewController *rsaVC = [storyboard instantiateViewControllerWithIdentifier:@"rsaViewController"];
                [self.navigationController pushViewController:rsaVC animated:YES];
            }
        }];
        [tempArr addObject:model];
    }
    [XHPopMenu showMenuWithView:self.typeButton menuItems:tempArr withOptions:nil];
}





@end
